package view.bookAdmiView;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import dao.BookDao;
import dao.BookTypeDao;
import model.Book;
import model.BookType;
import util.DbUtil;
import util.StringUtil;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
import javax.swing.ImageIcon;

public class BookAddInterFrm extends JInternalFrame {
	private JTextField bookNameTxt;
	private JTextField authorTxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField priceTxt;
	private JComboBox bookTypeJcb;
	private JTextArea bookDescTxt;
	private JRadioButton manJrb;
	private JRadioButton femaleJrb ;
	private JRadioButton notBorrowedJrb ;
	private JRadioButton borrowedJrb;
	
	
	private DbUtil dbUtil=new DbUtil();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private BookDao bookDao=new BookDao();
	private final ButtonGroup buttonGroup_1 = new ButtonGroup();
	
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookAddInterFrm frame = new BookAddInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookAddInterFrm() {
		getContentPane().setBackground(new Color(224, 255, 255));
		setBackground(new Color(175, 238, 238));
		setClosable(true);
		setIconifiable(true);
		setTitle("图书添加");
		setBounds(100, 100, 977, 759);
		
		JLabel bookName = new JLabel("图书名称：");
		bookName.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		bookNameTxt.setColumns(10);
		
		JLabel author = new JLabel("图书作者：");
		author.setFont(new Font("宋体", Font.BOLD, 25));
		
		authorTxt = new JTextField();
		authorTxt.setFont(new Font("宋体", Font.BOLD, 25));
		authorTxt.setColumns(10);
		
		JLabel sex = new JLabel("作者性别：");
		sex.setFont(new Font("宋体", Font.BOLD, 25));
		
		manJrb = new JRadioButton("男");
		manJrb.setBackground(new Color(224, 255, 255));
		manJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("女");
		femaleJrb.setBackground(new Color(224, 255, 255));
		femaleJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup.add(femaleJrb);
		
		JLabel price = new JLabel("图书价格：");
		price.setFont(new Font("宋体", Font.BOLD, 25));
		
		priceTxt = new JTextField();
		priceTxt.setFont(new Font("宋体", Font.BOLD, 25));
		priceTxt.setColumns(10);
		
		JLabel bookDesc = new JLabel("图书描述：");
		bookDesc.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookDescTxt = new JTextArea();
		bookDescTxt.setFont(new Font("宋体", Font.BOLD, 25));
		
		JLabel bookType = new JLabel("图书类别：");
		bookType.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookTypeJcb = new JComboBox();
		bookTypeJcb.setFont(new Font("宋体", Font.BOLD, 25));
		
		JButton addButton = new JButton("添加");
		addButton.setBackground(new Color(255, 255, 255));
		addButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/bookAdmi/添加 (2).png")));
		addButton.setFont(new Font("宋体", Font.BOLD, 25));
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookAddActionPerformed(e);
			}
		});
		
		JButton resetButton = new JButton("重置");
		resetButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/bookAdmi/重置.png")));
		resetButton.setFont(new Font("宋体", Font.BOLD, 25));
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		
		JLabel status = new JLabel("借阅状态：");
		status.setFont(new Font("宋体", Font.BOLD, 25));
		
		notBorrowedJrb = new JRadioButton("在馆");
		notBorrowedJrb.setBackground(new Color(224, 255, 255));
		notBorrowedJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup_1.add(notBorrowedJrb);
		notBorrowedJrb.setSelected(true);
		
		borrowedJrb = new JRadioButton("已借");
		borrowedJrb.setBackground(new Color(224, 255, 255));
		borrowedJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup_1.add(borrowedJrb);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(41)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(bookName)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGap(6)
							.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(author)
								.addComponent(price)
								.addComponent(status))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)
										.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
											.addComponent(borrowedJrb)
											.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(39)
									.addComponent(notBorrowedJrb)))
							.addGap(130))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(bookDesc)
									.addGap(37)
									.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 605, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(sex)
										.addGap(18)
										.addComponent(manJrb)
										.addGap(18)
										.addComponent(femaleJrb))
									.addGroup(groupLayout.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(bookType)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE))))
							.addContainerGap(211, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(235)
							.addComponent(addButton)
							.addGap(138)
							.addComponent(resetButton)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(53)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(author)
						.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookName)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(86)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(sex)
						.addComponent(manJrb)
						.addComponent(femaleJrb)
						.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(price))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(79)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(bookType)
								.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(84)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(status)
								.addComponent(notBorrowedJrb)
								.addComponent(borrowedJrb))))
					.addGap(77)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 204, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookDesc))
					.addGap(42)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(addButton)
						.addComponent(resetButton))
					.addContainerGap())
		);
		getContentPane().setLayout(groupLayout);

		//设置文本域边框
		bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
		
		//设置JFrame居中显示
		this.setLocation(500,20);
		
		
		fillBookType();
	}
	
	/**
	 * 重置事件处理
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent e) {
		this.reSetValue();
		
	}

	/**
	 * 图书添加事件处理
	 * @param e
	 */
	private void bookAddActionPerformed(ActionEvent evt) {
		String bookName=this.bookNameTxt.getText();
		String author=this.authorTxt.getText();
		String price=this.priceTxt.getText();
		String bookDesc=this.bookDescTxt.getText();
		if(StringUtil.isEmpty(bookName)) {
			JOptionPane.showMessageDialog(null, "图书名称不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(author)) {
			JOptionPane.showMessageDialog(null, "图书作者不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(price)) {
			JOptionPane.showMessageDialog(null, "图书价格不能为空!");
			return;
		}
		
		String sex="";
		
		if(manJrb.isSelected()) {
			sex="男";
			
		}
		else if(femaleJrb.isSelected()) {
			sex="女";
		}
		
		String status="";
		if(notBorrowedJrb.isSelected()) {
			status="在馆";
			
		}
		else if(borrowedJrb.isSelected()) {
			status="已借";
		}
		
		BookType bookType=(BookType) bookTypeJcb.getSelectedItem();
		int bookTypeId=bookType.getId();		
		Book book=new Book(bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc,status);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int addNum=bookDao.add(con, book);
			if(addNum==1) {
				JOptionPane.showMessageDialog(null, "图书添加成功!");
				reSetValue();
				
			}
			else {
				JOptionPane.showMessageDialog(null, "图书添加失败!");				
				
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "图书添加失败!");		
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
	}

	
	/**
	 * 重置表单
	 */
	private void reSetValue() {
		this.bookNameTxt.setText("");
		this.authorTxt.setText("");
		this.priceTxt.setText("");
		this.manJrb.setSelected(true);
		this.bookDescTxt.setText("");
		if(this.bookTypeJcb.getItemCount()>0) {
			this.bookTypeJcb.setSelectedIndex(0);
			
		}
	}

	/**
	 * 初始图书类别下拉框
	 */
	private void fillBookType() {
		Connection con=null;
		BookType bookType=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookTypeDao.list(con, new BookType());
			while(rs.next()) {
				bookType=new BookType();
				bookType.setId(rs.getInt("id"));
				bookType.setBookTypeName(rs.getString("bookTypeName"));
				this.bookTypeJcb.addItem(bookType);
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
		}
	}
}
